package cc.ufinity.networkdiagnose.view;

import java.util.Calendar;

import cc.ufinity.networkdiagnose.app.GlobalApp;

import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class WelcomeActivity extends AbstractActivity {

	//private ProgressBar pbar;
	private TextView version;
	private String strVersion;
	private Handler handler;
	
	private Dialog waitDialog;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		version = (TextView)findViewById(R.id.version);
		//pbar = (ProgressBar) this.findViewById(R.id.pBar);
		//注册服务
		//Intent service = new Intent(this,WifiScanService.class);
		//super.startService(service);
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				if(msg.arg1 == 1) {
					showVersion();
				}
			}
			
		};
		new Thread() {
			public void run() {
				init();
			}
		}.start();
	}

	private void init() {
		//if (NetworkManager.detectNetworkConnectivity(this, 0)) {
		 Calendar calendar = Calendar.getInstance();
	        long now = calendar.getTimeInMillis();//获得当前时间的毫秒表示
	        calendar.set(2013, 6, 30, 0, 0, 0);
	        long end = calendar.getTimeInMillis();
	        if(now > end) {
	        	GlobalApp.canUse = false;
	        }
			delay();
		/*} else {
			Toast.makeText(WelcomeActivity.this, "目前手机没有连接网络，请开启3G/2G或 WLAN网络",
					5000).show();

			// 设置扫描WLAN热点句柄,超时就停止扫描
			Handler wifiReceiverHandler = new Handler() {
				public void handleMessage(Message msg) {
					switch (msg.what) {
					case 1:
						// setScanTimout(true);
						pbar.setVisibility(View.GONE);
						Toast.makeText(WelcomeActivity.this,
								"附近没有WIFI网络热点，请选择其他地点重试或开启3G/2G！", 5000).show();
						break;
					}
					 super.handleMessage(msg);
				}
			};
			wlanManager = new WlanManager(this);
			wlanManager.setWifiReceiverHandler(wifiReceiverHandler);
			wlanManager.openWifi();
			wlanManager.startScan();
			wlanManager.startScanTimer();
		}*/
	}
	
	private void enter(){
//		Intent main = new Intent(WelcomeActivity.this, MainActivity.class);
			Intent main = new Intent(WelcomeActivity.this, HomePageActivity.class);
		//Intent main = new Intent(WelcomeActivity.this, MainActivity.class);
		main.putExtra("intentFlag", "WelcomeActivity");
		startActivity(main);
		this.finish();
	}

	private void delay() {
		PackageManager packageManager = getPackageManager();
		//getPackageName()是你当前类的包名，0代表是获取版本信息
		PackageInfo packInfo = null;
		try {
			packInfo = packageManager.getPackageInfo(getPackageName(), 0);
		} catch (NameNotFoundException e) {}
		if(packInfo != null) {
			strVersion = packInfo.versionName; 
		}
		if(strVersion != null) {
			Message msg = handler.obtainMessage();
			msg.arg1 = 1;
			msg.sendToTarget();
			//version.setText("版本号：" + v);
		}

		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {}
		enter();
	
	}
	
	private void showVersion() {
    	waitDialog = new Dialog(WelcomeActivity.this, R.style.TRANSDIALOG);
        waitDialog.setContentView(R.layout.version);
        waitDialog.getWindow().setBackgroundDrawableResource(R.color.transparent2);
        version = (TextView)waitDialog.findViewById(R.id.version);
        version.setText("版本号：" + strVersion);
      //设置窗口里控件的透明度
        Window window = waitDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        window.setGravity(Gravity.CENTER_HORIZONTAL| Gravity.TOP);

        WindowManager m = WelcomeActivity.this.getWindowManager(); 
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用 
        //lp.x = (d.getWidth()- 200) / 2; // 新位置X坐标 480
        lp.y = (int) (d.getHeight() * 0.87); // 新位置Y坐标 800
        lp.alpha = 0.95f; // 透明度 
        window.setAttributes(lp); 
        waitDialog.setCancelable(false);
        waitDialog.setCanceledOnTouchOutside(false);
        waitDialog.show();
    }

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if(null != waitDialog) {
			waitDialog.dismiss();
		}
		super.onDestroy();
	}

}
